package com.kly.smartbi.test.mylock;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 自旋锁
 * @author admin
 * @date 2025/4/8
 */
public class CasLockDemo {

    private final AtomicReference<Thread> atomicReference = new AtomicReference<>();
    public static void main(String[] args) {

    }

    public void tryLock(){
        Thread thread = Thread.currentThread();
        while (!atomicReference.compareAndSet(null, thread)) {

        }
    }

    public void unLock() {
        Thread thread = Thread.currentThread();
        atomicReference.compareAndSet(thread, null);
    }
}
